import axios from "axios";
import { Message } from "element-ui";
import store from "@/store/index";

const http = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 3000, // 3s
});

// 全局请求拦截器
axios.interceptors.request.use((config) => {
  const { authUser } = store.state.user;
  if (authUser && authUser.token) {
    config.headers["User-Token"] = authUser.token;
  }
  return config;
});

// 全局响应拦截器
http.interceptors.response.use(
  (response) => response.data,
  (err) => Message.error(err.response.data.msg || "响应报错了")
);

export default http;
